package com.sikao.app.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.MyConcernBean;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.utils.TimeUtil;

import java.util.List;

/**
 * Created by y50 on 2016/5/24.
 */
public class MyConcernedAdapter extends BaseAdapter {

	private Context context;
	private Callback cancelAttention;
	private List<MyConcernBean.ResBean.ListBean> list;

	public MyConcernedAdapter(Context context, Callback cancelAttention,List<MyConcernBean.ResBean.ListBean> list) {
		this.context = context;
		this.cancelAttention = cancelAttention;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder;
		if (convertView == null){
			view = View.inflate(context, R.layout.my_concerned_list_item, null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
			view = convertView;
		}
		holder.btnCancelAttention.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancelAttention.notice(position);
			}
		});
		holder.typeTv.setText(list.get(position).getTitle());
		holder.priceUnitTv.setText("元/"+list.get(position).getUnit());
		holder.priceTv.setText(list.get(position).getPrice());
		holder.quantityTv.setText("数量"+list.get(position).getNumber()+list.get(position).getUnit());
		holder.timeTv.setText(TimeUtil.timeStampToTime(list.get(position).getEnd_time(),"yyyy-MM-dd"));
		if (list.get(position).getIs_ported().equals("1")){
			holder.canSplitTv.setVisibility(View.VISIBLE);
		}else{
			holder.canSplitTv.setVisibility(View.GONE);
		}
		if ("1".equals(list.get(position).getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_orange);
			holder.releaseTypeTv.setText("仓单挂单");
		}
		if ("2".equals(list.get(position).getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_blue_light);
			holder.releaseTypeTv.setText("保证金挂单");
		}
		if ("3".equals(list.get(position).getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_yellow);
			holder.releaseTypeTv.setText("信誉挂单");
		}
		holder.deliveryPlaceTv.setText(list.get(position).getDelivery_area());
		holder.deliveryModeTv.setText(list.get(position).getDelivery_type_str());
		return view;
	}


	public class ViewHolder {
		public final TextView typeTv;
		public final TextView canSplitTv;
		public final TextView releaseTypeTv;
		public final TextView timeTv;
		//public final TextView nameTv;
		public final TextView priceTv;
		public final TextView priceUnitTv;
		public final TextView quantityTv;
		public final TextView deliveryPlaceTv;
		public final TextView deliveryModeTv;
		public final Button btnCancelAttention;
		public final View root;

		public ViewHolder(View root) {
			typeTv = (TextView) root.findViewById(R.id.type_tv);
			canSplitTv = (TextView) root.findViewById(R.id.can_split_tv);
			releaseTypeTv = (TextView) root.findViewById(R.id.release_type_tv);
			timeTv = (TextView) root.findViewById(R.id.time_tv);
			//nameTv = (TextView) root.findViewById(R.id.name_tv);
			priceTv = (TextView) root.findViewById(R.id.price_tv);
			priceUnitTv = (TextView) root.findViewById(R.id.price_unit_tv);
			quantityTv = (TextView) root.findViewById(R.id.quantity_tv);
			deliveryPlaceTv = (TextView) root.findViewById(R.id.delivery_place_tv);
			deliveryModeTv = (TextView) root.findViewById(R.id.delivery_mode_tv);
			btnCancelAttention = (Button) root.findViewById(R.id.btn_delete);
			this.root = root;
		}
	}
}
